﻿// By Conmajia<conmajia@gmail.com> on September 6th., 2012
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace CSharpGo
{
    public class World
    {
        const int PADDING = 20;

        #region Variables
        List<Bottle> bottles = new List<Bottle>();

        Rectangle bounds = Rectangle.Empty;
        #endregion

        #region Properties
        public List<Bottle> Bottles
        {
            get { return bottles; }
        }

        public Rectangle Bounds
        {
            get { return bounds; }
            set
            {
                bounds = value;
                arrangeBottles();
            }
        }
        #endregion

        #region Initiators
        public World()
        {

        }
        public World(Rectangle bounds)
        {
            this.bounds = bounds;
        }
        #endregion

        #region world methods
        public Bottle CreateBottle(int capacity)
        {
            return CreateBottle(capacity, 0);
        }
        public Bottle CreateBottle(int capacity, int content)
        {
            Bottle b = new Bottle(capacity, content);
            bottles.Add(b);
            arrangeBottles();
            return b;
        }

        public void DestroyBottle()
        {
            bottles.Clear();
        }
        public void DestroyBottle(Bottle b)
        {
            bottles.Remove(b);
        }
        public void DestroyBottle(int capacity)
        {
            List<Bottle> tmp = new List<Bottle>();
            foreach (Bottle b in bottles)
            {
                if (b.Capacity != capacity)
                    tmp.Add(b);
            }

            bottles.Clear();
            bottles.AddRange(tmp);
        }

        #endregion

        #region paint helpers
        Size getTotalSize()
        {
            Size sz = Size.Empty;
            foreach (Bottle b in bottles)
            {
                sz.Width += PADDING + b.Bounds.Width;
                if (sz.Height < b.Bounds.Height)
                    sz.Height = b.Bounds.Height;
            }

            return sz;
        }

        void arrangeBottles()
        {
            Size sz = getTotalSize();
            Point offset = new Point(
                (bounds.Width - sz.Width) / 2,
                (bounds.Height - sz.Height) / 2 + sz.Height
                );

            foreach (Bottle b in bottles)
            {
                b.X = offset.X;
                b.Y = offset.Y - b.Bounds.Height;

                offset.X += PADDING + b.Bounds.Width;
            }
        }
        #endregion

    }
}
